package net.sourceforge.zbar.android;

import com.srt.decoder.DecodeResult;
import com.srt.decoder.Decoder;
import com.srt.decoder.DecoderConfigValues.SymbologyID;

/**
 * Created by xiaox on 2016/12/1.
 */

public class NewtologicDecodeModule implements DecodeModule {
    private final DecodeResult mDecodeResult;
    private final Decoder mDecoder;
    private boolean isInit = false;

    NewtologicDecodeModule() {
        mDecodeResult = new DecodeResult();
        mDecoder = new Decoder();
    }

    @Override
    public String decode(byte[] data, int width, int height) {
        if (!isInit) {
            int ret = mDecoder.InitDecoderEngine(width, height);
            if (ret != 1) return null;
            isInit = true;
            mDecoder.setDecodeSearchLimit(200);
            mDecoder.setDecodeAttemptLimit(400);
            mDecoder.enableSymbology(SymbologyID.CODABAR);
            mDecoder.enableSymbology(SymbologyID.CODE39);
            mDecoder.enableSymbology(SymbologyID.CODE93);
            mDecoder.enableSymbology(SymbologyID.CODE128);
            mDecoder.enableSymbology(SymbologyID.PDF417);
            mDecoder.enableSymbology(SymbologyID.QR);
            mDecoder.enableSymbology(SymbologyID.DATAMATRIX);
            mDecoder.enableSymbology(SymbologyID.RSS);
            mDecoder.enableSymbology(SymbologyID.EAN8);
            mDecoder.enableSymbology(SymbologyID.UPCE0);
            mDecoder.enableSymbology(SymbologyID.UPCE1);
            mDecoder.enableSymbology(SymbologyID.UPCA);
            mDecoder.enableSymbology(SymbologyID.EAN13);
            mDecoder.enableSymbology(SymbologyID.COMPOSITE);
            mDecoder.enableSymbology(SymbologyID.EAN8);
            mDecoder.enableSymbology(SymbologyID.UPCE0);
            mDecoder.enableSymbology(SymbologyID.UPCE1);
            mDecoder.enableSymbology(SymbologyID.UPCA);
            mDecoder.enableSymbology(SymbologyID.EAN13);
            mDecoder.enableSymbology(SymbologyID.COMPOSITE);
        }
        mDecoder.RunDecodeImage(data, mDecodeResult, width, height);
        return mDecodeResult.length > 0 ? new String(mDecodeResult.byteBarcodeData, 0, mDecodeResult.length) : null;
    }

    @Override
    public void close() {
        mDecoder.disconnectFromDecoder();
        isInit = false;
    }
}
